package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcAttachment;
import com.visizen.wisdomclass.server.model.WcCourse;
import com.visizen.wisdomclass.server.model.WcHomework;
import com.visizen.wisdomclass.server.model.WcHomeworkExample;
import com.visizen.wisdomclass.server.model.dto.HomeworkExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface WcHomeworkMapper {
    /**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@SelectProvider(type = WcHomeworkSqlProvider.class, method = "countByExample")
	int countByExample(WcHomeworkExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@DeleteProvider(type = WcHomeworkSqlProvider.class, method = "deleteByExample")
	int deleteByExample(WcHomeworkExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@Delete({ "delete from wc_homework",
			"where homework_id = #{homework_id,jdbcType=BIGINT}" })
	int deleteByPrimaryKey(Long homework_id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@Insert({
			"insert into wc_homework (homework_id, organization_id, ",
			"course_id, teacher_user_id, ",
			"name, createtime, ",
			"leaddate, homework_date, ",
			"isfeedback, homework_text)",
			"values (#{homework_id,jdbcType=BIGINT}, #{organization_id,jdbcType=BIGINT}, ",
			"#{course_id,jdbcType=BIGINT}, #{teacher_user_id,jdbcType=BIGINT}, ",
			"#{name,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, ",
			"#{leaddate,jdbcType=DATE}, #{homework_date,jdbcType=DATE}, ",
			"#{isfeedback,jdbcType=INTEGER}, #{homework_text,jdbcType=LONGVARCHAR})" })
	int insert(WcHomework record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@InsertProvider(type = WcHomeworkSqlProvider.class, method = "insertSelective")
	int insertSelective(WcHomework record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@SelectProvider(type = WcHomeworkSqlProvider.class, method = "selectByExampleWithBLOBs")
	@Results({
			@Result(column = "homework_id", property = "homework_id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "organization_id", property = "organization_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "course_id", property = "course_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "teacher_user_id", property = "teacher_user_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
			@Result(column = "leaddate", property = "leaddate", jdbcType = JdbcType.DATE),
			@Result(column = "homework_date", property = "homework_date", jdbcType = JdbcType.DATE),
			@Result(column = "isfeedback", property = "isfeedback", jdbcType = JdbcType.INTEGER),
			@Result(column = "homework_text", property = "homework_text", jdbcType = JdbcType.LONGVARCHAR) })
	List<WcHomework> selectByExampleWithBLOBs(WcHomeworkExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@SelectProvider(type = WcHomeworkSqlProvider.class, method = "selectByExample")
	@Results({
			@Result(column = "homework_id", property = "homework_id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "organization_id", property = "organization_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "course_id", property = "course_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "teacher_user_id", property = "teacher_user_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
			@Result(column = "leaddate", property = "leaddate", jdbcType = JdbcType.DATE),
			@Result(column = "homework_date", property = "homework_date", jdbcType = JdbcType.DATE),
			@Result(column = "isfeedback", property = "isfeedback", jdbcType = JdbcType.INTEGER) })
	List<WcHomework> selectByExample(WcHomeworkExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@Select({
			"select",
			"homework_id, organization_id, course_id, teacher_user_id, name, createtime, ",
			"leaddate, homework_date, isfeedback, homework_text",
			"from wc_homework",
			"where homework_id = #{homework_id,jdbcType=BIGINT}" })
	@Results({
			@Result(column = "homework_id", property = "homework_id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "organization_id", property = "organization_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "course_id", property = "course_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "teacher_user_id", property = "teacher_user_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
			@Result(column = "leaddate", property = "leaddate", jdbcType = JdbcType.DATE),
			@Result(column = "homework_date", property = "homework_date", jdbcType = JdbcType.DATE),
			@Result(column = "isfeedback", property = "isfeedback", jdbcType = JdbcType.INTEGER),
			@Result(column = "homework_text", property = "homework_text", jdbcType = JdbcType.LONGVARCHAR) })
	WcHomework selectByPrimaryKey(Long homework_id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcHomeworkSqlProvider.class, method = "updateByExampleSelective")
	int updateByExampleSelective(@Param("record") WcHomework record,
			@Param("example") WcHomeworkExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcHomeworkSqlProvider.class, method = "updateByExampleWithBLOBs")
	int updateByExampleWithBLOBs(@Param("record") WcHomework record,
			@Param("example") WcHomeworkExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcHomeworkSqlProvider.class, method = "updateByExample")
	int updateByExample(@Param("record") WcHomework record,
			@Param("example") WcHomeworkExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcHomeworkSqlProvider.class, method = "updateByPrimaryKeySelective")
	int updateByPrimaryKeySelective(WcHomework record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@Update({ "update wc_homework",
			"set organization_id = #{organization_id,jdbcType=BIGINT},",
			"course_id = #{course_id,jdbcType=BIGINT},",
			"teacher_user_id = #{teacher_user_id,jdbcType=BIGINT},",
			"name = #{name,jdbcType=VARCHAR},",
			"createtime = #{createtime,jdbcType=TIMESTAMP},",
			"leaddate = #{leaddate,jdbcType=DATE},",
			"homework_date = #{homework_date,jdbcType=DATE},",
			"isfeedback = #{isfeedback,jdbcType=INTEGER},",
			"homework_text = #{homework_text,jdbcType=LONGVARCHAR}",
			"where homework_id = #{homework_id,jdbcType=BIGINT}" })
	int updateByPrimaryKeyWithBLOBs(WcHomework record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_homework
	 * @mbggenerated
	 */
	@Update({ "update wc_homework",
			"set organization_id = #{organization_id,jdbcType=BIGINT},",
			"course_id = #{course_id,jdbcType=BIGINT},",
			"teacher_user_id = #{teacher_user_id,jdbcType=BIGINT},",
			"name = #{name,jdbcType=VARCHAR},",
			"createtime = #{createtime,jdbcType=TIMESTAMP},",
			"leaddate = #{leaddate,jdbcType=DATE},",
			"homework_date = #{homework_date,jdbcType=DATE},",
			"isfeedback = #{isfeedback,jdbcType=INTEGER}",
			"where homework_id = #{homework_id,jdbcType=BIGINT}" })
	int updateByPrimaryKey(WcHomework record);

	/**
     * 根据班级ID科目ID获取新的次作业ID
     *
     * @param classId
     * @param courseId
     * @return
     */
    @Select("SELECT homework_id FROM wc_homework " +
            " WHERE organization_id=#{classId} AND course_id=#{courseId} ORDER BY createtime DESC LIMIT 0,1 ")
    Long selectLastWorkId(@Param("classId") Long classId, @Param("courseId") Long courseId);

    /**
     * 获取某班级的作业信息
     *
     * @param classId
     * @param start
     * @param count
     * @return
     */
    @Select("SELECT * FROM wc_homework WHERE organization_id=#{classId} LIMIT #{start},#{count}")
    @Results({
            @Result(column = "course_id", property = "course_id"),
            @Result(column = "homework_id", property = "homework_id"),
            @Result(column = "organization_id", property = "organization_id"),
            @Result(property = "course", column = "course_id", one = @One(select = "selectCourse")),
            @Result(property = "viewNum", column = "homework_id", one = @One(select = "selectViewCount")),
            @Result(property = "viewTotalNum", column = "organization_id", one = @One(select = "selectTotalCount")),
            @Result(property = "feedbackNum", column = "homework_id", one = @One(select = "selectFeedbackCount")),
            @Result(property = "feedbackTotalNum", column = "organization_id", one = @One(select = "selectTotalCount"))

    })
    List<HomeworkExt> selectByClassId(@Param("classId") Long classId, @Param("start") int start, @Param("count") int count);

    /**
     * 通过作业ID获取作业的详情信
     *
     * @param homeworkId
     * @return
     */
    @Select("SELECT * FROM wc_homework WHERE homework_id=#{homeworkId}")
    @Results({
            @Result(column = "course_id", property = "course_id"),
            @Result(column = "homework_id", property = "homework_id"),
            @Result(column = "organization_id", property = "organization_id"),
            @Result(property = "course", column = "course_id", one = @One(select = "selectCourse")),
            @Result(property = "viewNum", column = "homework_id", one = @One(select = "selectViewCount")),
            @Result(property = "viewTotalNum", column = "organization_id", one = @One(select = "selectTotalCount")),
            @Result(property = "feedbackNum", column = "homework_id", one = @One(select = "selectFeedbackCount")),
            @Result(property = "feedbackTotalNum", column = "organization_id", one = @One(select = "selectTotalCount")),
            @Result(property = "attachments", javaType = List.class, column = "homework_id", many = @Many(select = "selectAttachments"))
    })
    HomeworkExt selectById(@Param("homeworkId") long homeworkId);

    @Select(" select * from wc_attachment\n" +
            "  where attach_id in (select attach_id from wc_homework_attach where homework_id=#{homeworkId})")
    List<WcAttachment> selectAttachments(@Param("homeworkId") Long homeworkId);

    /**
     * 获取作业的查看数
     *
     * @param homeworkId
     * @return
     */
    @Select("SELECT COUNT(*) FROM wc_homework_log_see WHERE homework_id=#{homeworkId}")
    int selectViewCount(@Param("homeworkId") Long homeworkId);

    /**
     * 获取作业的反馈数
     *
     * @param homeworkId
     * @return
     */
    @Select("SELECT COUNT(*) FROM wc_homework_feedback WHERE homework_id=#{homeworkId}")
    int selectFeedbackCount(@Param("homeworkId") Long homeworkId);

    /**
     * 获取班级当前主要联系人
     *
     * @param classId
     * @return
     */
    @Select("  SELECT\n" +
            "    COUNT(*)\n" +
            "  FROM wc_family\n" +
            "  WHERE family_user_id IN (\n" +
            "    SELECT\n" +
            "      user_id\n" +
            "    FROM wc_organization_user\n" +
            "    WHERE organization_id = #{organizationId} AND role_id = 7) AND ismain = 1;")
    int selectTotalCount(@Param("organizationId") Long classId);

    @Select("SELECT * FROM wc_course WHERE course_id=#{courseId}")
    WcCourse selectCourse(@Param("courseId") Long courseId);

    /**
     * 查询教师与作业是否对
     *
     * @param teacherId
     * @param homeworkId
     * @return
     */
    @Select("SELECT COUNT(*) FROM wc_homework WHERE homework_id=#{homeworkId} and teacher_user_id=#{teacherId}")
    int selectByTeacherIdHomeworkId(@Param("teacherId") Long teacherId,
                                    @Param("homeworkId") Long homeworkId);

    @Insert({
            "insert into wc_homework (homework_id, organization_id, ",
            "course_id, teacher_user_id, ",
            "NAME, createtime, ",
            "leaddate, homework_date, ",
            "isfeedback, homework_text)",
            "values (#{homework_id,jdbcType=BIGINT}, #{organization_id,jdbcType=BIGINT}, ",
            "#{course_id,jdbcType=BIGINT}, #{teacher_user_id,jdbcType=BIGINT}, ",
            "#{NAME,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, ",
            "#{leaddate,jdbcType=DATE}, #{homework_date,jdbcType=DATE}, ",
            "#{isfeedback,jdbcType=INTEGER}, #{homework_text,jdbcType=LONGVARCHAR})"})
    @Options(useGeneratedKeys = true, keyProperty = "homework_id", keyColumn = "homework_id")
    int insertAndGenerateKeys(WcHomework record);
}